<?php

/**
 * curso actions.
 *
 * @package    oacademy
 * @subpackage curso
 * @author     Otto Giron
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class courseActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
   
  	$maxCoursesPerPage=sfConfig::get("app_max_courses_on_homepage");  	
    $currentPage=$request->getParameter('page', 1);
    $latestCoursesQuery=Doctrine_Core::getTable('Course')->getLatestCoursesQuery();
    
    $this->pager=new sfDoctrinePager('Course',
            $maxCoursesPerPage);    
    $this->pager->setQuery($latestCoursesQuery);   
    $this->pager->setPage($currentPage);
    $this->pager->init();     
  }
  

  public function executeListLectures(sfRequest $request)
  {  	
  	$this->course=$this->getRoute()->getObject();
    $this->lectures=$this->course->getLectures();
      
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($o_course = Doctrine_Core::getTable('Course')->find(array($request->getParameter('id'))), sprintf('Object o_course does not exist (%s).', $request->getParameter('id')));
    $this->form = new CourseForm($o_course);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($o_course = Doctrine_Core::getTable('Course')->find(array($request->getParameter('id'))), sprintf('Object o_course does not exist (%s).', $request->getParameter('id')));
    $this->form = new CourseForm($o_course);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($o_course = Doctrine_Core::getTable('Course')->find(array($request->getParameter('id'))), sprintf('Object o_course does not exist (%s).', $request->getParameter('id')));
    $o_course->delete();

    $this->redirect('curso/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $o_course = $form->save();

      $this->redirect('curso/edit?id='.$o_course->getId());
    }
  }
}
